/// <summary>
/// 语音控制器,会在识别到关键词后让小人执行相应的动作,同时触发肌肉电刺激器
/// </summary>
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Windows.Speech;

public class voiceControler : MonoBehaviour
{
    public Animation animation;
    public Slider slider;
    private float speed = 1f;
    // 短语识别器
    private PhraseRecognizer m_PhraseRecognizer;
    // 关键字
    private string[] keywords = { "抬手", "放手", "开机", "关机", "增强刺激" };
    // 可信度
    public ConfidenceLevel m_confidenceLevel = ConfidenceLevel.Medium;

    public muscleStimulator muscleStimulator;
    public JointControl jointControl;
    /// <summary>
    /// 显示的文字
    /// </summary>
    public Text ShowText;
    // Use this for initialization
    void Start()
    {
        // ShowText = GameObject.Find("ShowText").GetComponent<Text>();

        if (m_PhraseRecognizer == null)
        {
            //创建一个识别器
            m_PhraseRecognizer = new KeywordRecognizer(keywords, m_confidenceLevel);
            //通过注册监听的方法
            m_PhraseRecognizer.OnPhraseRecognized += M_PhraseRecognizer_OnPhraseRecognized;
            //开启识别器
            m_PhraseRecognizer.Start();

            Debug.Log("创建识别器成功");
        }
    }

    /// <summary>
    ///  当识别到关键字时，会调用这个方法
    /// </summary>
    /// <param name="args">识别到关键字</param>
    private void M_PhraseRecognizer_OnPhraseRecognized(PhraseRecognizedEventArgs args)
    {
        SpeechRecognition(args.text);
        ShowText.text = "识别内容：" + args.text;
        Debug.Log(args.text);
    }
    private void OnDestroy()
    {
        //判断场景中是否存在语音识别器，如果有，释放
        if (m_PhraseRecognizer != null)
        {
            //用完应该释放，否则会带来额外的开销
            m_PhraseRecognizer.Dispose();
        }

    }
    // Update is called once per frame
    void Update()
    {
    }
    /// <summary>
    /// 识别到语音的操作
    /// </summary>
    public void SpeechRecognition(string s)
    {
        if (s == keywords[0])
        {
            jointControl.onAction1();
            muscleStimulator.DoMotion(muscleStimulator.MOTION.RAISE_ARM);

        }
        else if (s == keywords[1])
        {
            jointControl.onAction2();
            muscleStimulator.DoMotion(muscleStimulator.MOTION.DROP_ARM);


        }
        else if (s == keywords[2])
        {
            muscleStimulator.sendCommand(muscleStimulator.MC_COMMAND.MC_On);
        }
        else if (s == keywords[3])
        {
            muscleStimulator.sendCommand(muscleStimulator.MC_COMMAND.MC_Off);

        }
        else if (s == keywords[4])
        {
            muscleStimulator.sendCommand(muscleStimulator.MC_COMMAND.MC_increaseStrength);

        }

    }
}
